﻿using System.Collections.Generic;
using DomainModel;
using NHibernate.Transform;
using Repository.Abstract;

namespace Repository.NHibernate
{
    public class TimetableUnitRepository : Repository<TimetableUnit>, ITimetableUnitRepository
    {
        public IEnumerable<User> GetTeachersBySubjectId(long subjectId)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(t => t.Subject.Id == subjectId).Select(t => t.Teacher).
                    TransformUsing(Transformers.DistinctRootEntity).List<User>();
        }

        public TimetableUnit GetByWeekDayPairNoGroup(StudySemester semester, int week, int day, int pairNo, Group group)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Group == group).SingleOrDefault();
        }

        public TimetableUnit GetByWeekDayPairNoGroupTeacherSubject(StudySemester semester, int week, int day, int pairNo, Group group, User teacher, Subject subject)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Group == group && t.Subject == subject && t.Teacher == teacher).SingleOrDefault();
        }

        public TimetableUnit GetByWeekDayPairNoGroupTeacher(StudySemester semester, int week, int day, int pairNo, Group group, User teacher)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Group == group && t.Teacher == teacher).SingleOrDefault();
        }

        public TimetableUnit GetByWeekDayPairNoGroupSubject(StudySemester semester, int week, int day, int pairNo, Group group, Subject subject)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Group == group && t.Subject == subject).SingleOrDefault();
        }

        public IEnumerable<TimetableUnit> GetByWeekDayPairNoTeacher(StudySemester semester, int week, int day, int pairNo, User teacher)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Teacher == teacher).List();
        }

        public IEnumerable<TimetableUnit> GetByWeekDayPairNoRoom(StudySemester semester, int week, int day, int pairNo, string room)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo && t.Room == room).List();
        }

        public IEnumerable<TimetableUnit> GetByWeekDayPairNo(StudySemester semester, int week, int day, int pairNo)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(
                    t => t.StudySemester == semester && t.Week == week && t.Day == day && t.PairNo == pairNo).List();
        }

        public IEnumerable<Subject> GetSubjectsByTeacher(User teacher)
        {
            return
                Session.QueryOver<TimetableUnit>().Where(t => t.Teacher.Id == teacher.Id).Select(t => t.Subject).
                    TransformUsing(Transformers.DistinctRootEntity).List<Subject>();
        }
    }
}